package com.hualan.sfzy.controller.course;

import com.hualan.sfzy.entity.course.CourseStore;
import com.hualan.sfzy.service.course.CourseStoreService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class CourseStoreController {
    @Autowired
    private CourseStoreService courseStoreService;
    @PostMapping("/courseStore")
    public Result add(@RequestBody CourseStore courseStore) {
        return courseStoreService.add(courseStore) >0 ? Result.success():Result.error();
    }
    @DeleteMapping("/courseStore/{id}")
    public Result remove(@PathVariable int id) {
        return courseStoreService.remove(id) >0 ?Result.success():Result.error();
    }
    @DeleteMapping("/courseStore")
    public Result removeBatch(Integer[] ids) {
        return courseStoreService.removeBatch(ids) > 0 ?Result.success():Result.error();
    }
    @PutMapping("/courseStore")
    public Result edit(@RequestBody CourseStore courseStore) {
        return courseStoreService.edit(courseStore) >0 ?Result.success():Result.error();
    }
    @GetMapping("/courseStore")
    public Result find() {
        List<CourseStore> list = courseStoreService.find();
        return Result.success(list);
    }
    @GetMapping("/courseStore/{id}")
    public Result findById(@PathVariable int id) {
        CourseStore courseStore = courseStoreService.findById(id);
        return Result.success(courseStore);
    }
    @GetMapping("/courseStore/page")
    public Result findByPage(
            @RequestParam(value = "pageNum",defaultValue = "1",required = false) Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5",required = false)
            Integer pageSize
            ,CourseStore courseStore) {
        Map<String,Object> map = courseStoreService.findByPage(pageNum, pageSize, courseStore);
        return Result.success(map);
    }

    @GetMapping("/courseStore/count")
    public Result getCollectionCount(@RequestParam Integer courseId) {
        int count = courseStoreService.countByCourseId(courseId);
        return Result.success(count);
    }



    @GetMapping("/courseStore/check")
    public Result checkCollection(@RequestParam Integer userId, @RequestParam Integer courseId) {
        Integer isCollected = courseStoreService.checkCollection(userId, courseId);
        return Result.success(isCollected);
    }

    @DeleteMapping("/courseStore/deleteByUserAndCourse")
    public Result deleteByUserAndCourse(@RequestParam Integer userId, @RequestParam Integer courseId) {
        int result = courseStoreService.deleteByUserAndCourse(userId, courseId);
        return result > 0 ? Result.success() : Result.error();
    }


}
